package org.example.utils;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.*;
import org.example.bean.dto.SortDto;
import org.example.dao.entity.DynamicViewColumn;

import java.util.ArrayList;
import java.util.List;

public class JSqlParserUtil {

    public static List<OrderByElement> getOrderByElements(List<SortDto> sortList) {
        List<OrderByElement> orderByElements = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(sortList)) {
            for (SortDto sortDto : sortList) {
                orderByElements.add(getOrderByElement(sortDto.getSortField(), sortDto.getSortType()));
            }
        }
        return orderByElements;
    }

    public static OrderByElement getOrderByElement(String sortField, String sortType) {
        return getOrderByElement(null, sortField, sortType);
    }

    public static OrderByElement getOrderByElement(Table table, String sortField, String sortType) {
        OrderByElement orderByElement = new OrderByElement();
        orderByElement.setExpression(new Column(table, sortField));
        orderByElement.setAsc(asc(sortType));
        return orderByElement;
    }

    public static boolean asc(String sortType) {
        return sortType.trim().equalsIgnoreCase("asc");
    }

    public static Join getMaintainLevelJoin(Table maintainTable, Table maintainLevelTable) {
        Join join = new Join();
        join.setInner(true);
        join.setRightItem(maintainLevelTable);
        EqualsTo equalsTo = new EqualsTo();
        equalsTo.setLeftExpression(new Column(maintainTable, "maintain_id"));
        equalsTo.setRightExpression(new Column(maintainLevelTable, "maintain_id"));
        join.setOnExpression(equalsTo);
        return join;
    }

    public static Join getMaintainJoin(Table t1, Table maintainTable) {
        Join join = new Join();
        join.setInner(true);
        join.setRightItem(maintainTable);
        EqualsTo equalsTo = new EqualsTo();
        equalsTo.setLeftExpression(new Column(t1, "merchant_id"));
        equalsTo.setRightExpression(new Column(maintainTable, "merchant_id"));
        join.setOnExpression(equalsTo);
        return join;
    }

    public static Join getUserMapJoin(Table t1, Table userTable) {
        Join join = new Join();
        join.setInner(true);
        join.setRightItem(userTable);
        EqualsTo equalsTo = new EqualsTo();
        equalsTo.setLeftExpression(new Column(t1, "merchant_id"));
        equalsTo.setRightExpression(new Column(userTable, "merchant_id"));
        join.setOnExpression(equalsTo);
        return join;
    }

    public static List<SelectItem> getSelectItems(Table table, List<DynamicViewColumn> viewColumns) {
        List<SelectItem> selectItemList = new ArrayList<>();
        for (DynamicViewColumn column : viewColumns) {
            SelectExpressionItem selectItem = new SelectExpressionItem(new Column(table, column.getColumnName()));
            if (StringUtils.isNotBlank(column.getColumnAlias())) {
                selectItem.setAlias(new Alias(column.getColumnAlias()));
            }
            if (StringUtils.isNotBlank(column.getAggFunc())) {
                Function function = new Function();
                function.setName(column.getAggFunc());
                function.setParameters(new ExpressionList(new Column(table, column.getColumnName())));
                selectItem.setExpression(function);
            }
            selectItemList.add(selectItem);
        }
        return selectItemList;
    }
}
